define(['zepto', 'butterfly/view','../../bsl',
    'text!./xhr.html'
], function($, View, bsl, xhr) {
    var me = null,xhr;

    var view = View.extend({
        events: {

            // ！！！！ 由于 引入 touch.js 后，tap事件会变成双击，所以 本页面内不能使用tap，用click代替。
            "tap #openDoc" : "OpenDoc",
            "tap #back" : "back",
            "tap #xhrCreate" : "xhrCreate",
            "tap #xhrResponseHeader" : "xhrResponseHeader",
            "tap #xhrAbort" : "xhrAbort",

      
        },
        render: function() {
            me = this;
        },

        onShow: function() {

            // 绑定自定义事件 滑动切换日历等           


        },
         url:"http://www.dcloud.io/",


        xhrCreate:function () {
            if ( xhr ) {
                 console.info( "xhr请求已创建" );
                return;
            }
             console.info( "创建请求：" );
            xhr = new bsl.net.XMLHttpRequest();
            xhr.onreadystatechange = function () {
                switch ( xhr.readyState ) {
                    case 0:
                         console.info( "xhr请求已初始化" );
                    break;
                    case 1:
                         console.info( "xhr请求已打开" );
                    break;
                    case 2:
                         console.info( "xhr请求已发送" );
                    break;
                    case 3:
                         console.info( "xhr请求已响应");
                        break;
                    case 4:
                         console.info( "xhr请求已完成");
                        if ( xhr.status == 200 ) {
                             console.info( "xhr请求成功："+xhr.responseText );
                        } else {
                             console.info( "xhr请求失败："+xhr.status );
                        }
                        break;
                    default :
                        break;
                }
            }
            xhr.open( "GET", url );
            xhr.send();
        },
        xhrResponseHeader:function () {
            if ( xhr ) {
                if ( xhr.readyState != 4 ) {
                     console.info( "xhr请求未完成" );
                } else if ( xhr.status != 200 ) {
                     console.info( "xhr请求失败："+xhr.readyState );
                } else {
                     console.info( "xhr请求响应头数据：" );
                     console.info( xhr.getAllResponseHeaders() );
                }
            } else {
                 console.info( "未发送请求" );
            }
        },
        xhrAbort:function () {
            if ( xhr ) {
                 console.info( "取消请求" );
                if ( xhr.readyState != 4 ) {
                    xhr.abort();
                }
                xhr = null;
            } else {
                 console.info( "未发送请求" );
            }
        },

        back : function(e){
                window.history.go(-1);
                },

        OpenDoc : function(e){
           
            window.butterfly.navigate('com.no5.cordovademo/doc/xhr', {
                trigger: true
            });
        },
           
                     
            });
    return view;
});
